<?php

namespace Home\Controller;

use Think\Controller;

class QuestionsController extends Controller {

    //显示选择题
    public function index() {
        //$memberModel = D('Member');
        $user = session('member');  //获取session中的值
        $sid = $_GET["id"];
        $uid = $user;
        $questionsModel = D('Questions');
        $gradeModel = D('Grade');
        $questionsList = $questionsModel->where("q_sid = $sid AND q_type = 1")
                ->order('q_id desc')->limit(10)
                ->select();
        $rand = md5(microtime(true) . '');
        //判断是否答题
        $map["mid"] = $uid;
        $map["sid"] = $sid;
        $grade = $gradeModel->field("qid")->where($map)->find();
            if (!empty($questionsList)) {
                foreach ($questionsList as $value) {
                    $data = array();
                    $data['qid'] = $value['q_id'];
                    $data['mid'] = $uid;
                    $data['sid'] = $sid;
                    $data["date"] = date("Y-m-d H:i:s");
                    $data['q_type'] = $value['q_type'];
                    $data['judge'] = 0;
                    $data["flag"] = $rand;
                    $gradeModel->add($data);
                }
            }
           
            $map["flag"] = $rand;
            $gradeList = $gradeModel->field("qid")->where($map)->select();
            //获取到题目的信息
            $questionList = array();
            foreach ($gradeList as $v) {
                $question = $questionsModel->where(array("q_id" => $v["qid"]))->find();
                $questionList = $question;
                      $i = $question['q_type_e'];
                      $list =json_decode($i,true);
                      if($list != null){
                         $t[] = array_merge($questionList,$list);
                     }else{
                        $t[] =$questionList;
                     }
                   
            }
            $this->assign('count',count($t));
            $this->assign('list', json_encode($t));
            $this->display('questions');
       

    }





//显示填空题
   public function questions2(){
      $user = session('member');  //获取session中的值
        $uid = $user;
        $memberModel = D('Member');
        $questionsModel = D('Questions');
        $gradeModel = D('Grade');
        $memberList = $memberModel->where("id = $uid")->find();
        $sid = $memberList['sid'];
        $questionsList = $questionsModel->where("q_sid = $sid AND q_type = 2")
                ->order('q_id desc')->limit(10)
                ->select();
        $rand = md5(microtime(true) . '');
        //判断是否答题
        $map["mid"] = $uid;
        //$map["sid"] = $sid;
       // $grade = $gradeModel->field("qid")->where($map)->find();
        //if (!empty($grade)) {  //开关
/*           if(empty($grade['answer_a']) || empty($grade['answer_b']) ){
                $gradeList = $gradeModel->field("qid")->where(array("mid" => $map["mid"] ) )->select();
                //获取到题目的信息
                $questionList = array();
                foreach ($gradeList as $v) {
                    $question = $questionsModel->where(array("q_id" => $v["qid"]))->find();
                    $questionList[] = $question;
                }
            print_r($questionList);
            exit;
                $this->assign('count',count($questionList));
                $this->assign('list', json_encode($questionList));
                $this->display('questions2');
           }else{
            echo "该科目已经考试了";
           }*/
            
       // } else {
            if (!empty($questionsList)) {
                foreach ($questionsList as $value) {
                    $data = array();
                    $data['qid'] = $value['q_id'];
                    $data['mid'] = $uid;
                    $data['sid'] = $sid;
                    $data["date"] = date("Y-m-d H:i:s");
                    $data['q_type'] = $value['q_type'];
                    $data['judge'] = 0;
                    $data["flag"] = $rand;
                    $gradeModel->add($data);
                }
            }
           
            $map["flag"] = $rand;
            $gradeList = $gradeModel->field("qid")->where($map)->select();
            //获取到题目的信息
            $questionList = array();
            foreach ($gradeList as $v) {
                $question = $questionsModel->where(array("q_id" => $v["qid"]))->find();
                $questionList[] = $question;
            }
            $this->assign('count',count($questionList));
            $this->assign('list', json_encode($questionList));
            $this->display('questions2');
        //}
    }

    
    
    public function answer(){
        $data["answer_a"] = $_GET["answer"];  //答题人ID
        $map["qid"] = $_GET["question"]; //问题ID
        $map["sid"] = session('sid');//科目ID
        $gradeModel = D('Grade');
        $map["mid"] =session('member');
        $res= $gradeModel->where($map)->save($data);
        if($res!==false){
            $response["success"] = 1;
        }else{
            $response["success"] = 2;
        }
        echo json_encode($response);
     }


    public function answer2(){
        $data["answer_b"] = $_GET["answer"];  //答题人ID
        $map["qid"] = $_GET["question"]; //问题ID
        $map["sid"] = session('sid');//科目ID
        $gradeModel = D('Grade');
        $map["mid"] =session('member');
        $res= $gradeModel->where($map)->save($data);
        if($res!==false){
            $response["success"] = 1;
        }else{
            $response["success"] = 2;
        }
        echo json_encode($response);
     }

}